package com.ruoyi.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dto.zhwyAdvancedepositsShopIdAndcharId;
import com.ruoyi.mapper.ZhwyAdvancedepositMapper;
import com.ruoyi.mapper.ZhwyChargeitemMapper;
import com.ruoyi.mapper.ZhwyPaymentmethodMapper;
import com.ruoyi.mapper.ZhwyShopsMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ZhwyAdvancedeposit;
import com.ruoyi.service.IZhwyAdvancedepositService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预存款信息Controller
 * 
 * @author liuzhiqiang
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/feeDeposit/advancedeposit")
public class ZhwyAdvancedepositController extends BaseController
{
    @Autowired
    private IZhwyAdvancedepositService zhwyAdvancedepositService;
    @Autowired
    private ZhwyAdvancedepositMapper zhwyAdvancedepositMapper;
    @Autowired
    private ZhwyPaymentmethodMapper zhwyPaymentmethodMapper;
    @Autowired
    private ZhwyChargeitemMapper zhwyChargeitemMapper;
    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;
    //根据shopid查询预存款

    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:query')")
    @GetMapping("/shopId/{id}")
    public AjaxResult shopId(@PathVariable("id") Long id)
    {
        List<ZhwyAdvancedeposit> zhwyAdvancedeposits = zhwyAdvancedepositMapper.selectZhwyAdvancedepositByShopId(id);
        for(ZhwyAdvancedeposit zh:zhwyAdvancedeposits){
            zh.setChargeitemName(zhwyChargeitemMapper.selectZhwyChargeitemById(zh.getChargeitemId()).getName());
            zh.setPayName(zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zh.getPayId()).getName());
            zh.setRefundName(zhwyPaymentmethodMapper.selectZhwyPaymentmethodById(zh.getRefundId()).getName());
        }
        return success(zhwyAdvancedeposits);
    }
    //
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:query')")
    @GetMapping("/shopIdAndChargeItemid/{id}")
    public AjaxResult shopIdAndChargeItemid(@PathVariable("id") Long id)
    {
        List<Long> longs = zhwyAdvancedepositMapper.selectZhwyAdvancedepositByShopIdOne(id);
        List<zhwyAdvancedepositsShopIdAndcharId>list =new ArrayList();
        for(Long zh:longs){
            BigDecimal num = new BigDecimal("0.0");
            BigDecimal num1 = new BigDecimal("0.0");
            List<ZhwyAdvancedeposit> zhwyAdvancedeposits1 = zhwyAdvancedepositMapper.selectZhwyAdvancedepositByShopIdAndChargeItemId(id, zh);
            List<ZhwyAdvancedeposit> zhwyAdvancedeposits2 = zhwyAdvancedepositMapper.selectZhwyAdvancedepositByShopIdAndChargeItemId2(id, zh);
            zhwyAdvancedepositsShopIdAndcharId zhwyshop =new zhwyAdvancedepositsShopIdAndcharId();
                for(ZhwyAdvancedeposit zhwy:zhwyAdvancedeposits1){
                    //获取预存款金额
                    if(zhwy.getAmt() !=null){
                        num = num.add(zhwy.getAmt());
                    }
                }
                for(ZhwyAdvancedeposit zhwy:zhwyAdvancedeposits2){
                //获取预存款退款金额
                if(zhwy.getAmt() !=null){
                    num1 = num1.add(zhwy.getAmt());
                }
                }
                num =num.subtract(num1);
                zhwyshop.setMoney(num);
                zhwyshop.setChargeItemN(zhwyChargeitemMapper.selectZhwyChargeitemById(zh).getName());
                zhwyshop.setChargeItemId(zhwyAdvancedepositMapper.selectZhwyAdvancedepositById(zh).getChargeitemId());
                list.add(zhwyshop);

        }
        return success(list);
    }
    /**
     * 新增预存款信息
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:add')")
    @Log(title = "预存款信息", businessType = BusinessType.INSERT)
    @PostMapping("/addamts")
    public AjaxResult addamts(@RequestBody ZhwyAdvancedeposit zhwyAdvancedeposit)
    {
//        if(zhwyAdvancedeposit.getAmts() !=null){
//        for(BigDecimal bi:zhwyAdvancedeposit.getAmts()){
//            zhwyAdvancedeposit.setAmt(bi);
//            zhwyAdvancedeposit.setState(1L);
//            zhwyAdvancedepositService.insertZhwyAdvancedeposit(zhwyAdvancedeposit);
//        }
//
            zhwyAdvancedeposit.setState(1L);
            zhwyAdvancedepositService.insertZhwyAdvancedeposit(zhwyAdvancedeposit);
        return success();
    }
    /**
     * 查询预存款信息列表
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhwyAdvancedeposit zhwyAdvancedeposit)
    {
        startPage();
        List<ZhwyAdvancedeposit> list = zhwyAdvancedepositService.selectZhwyAdvancedepositList(zhwyAdvancedeposit);
        return getDataTable(list);
    }

    /**
     * 导出预存款信息列表
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:export')")
    @Log(title = "预存款信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhwyAdvancedeposit zhwyAdvancedeposit)
    {
        List<ZhwyAdvancedeposit> list = zhwyAdvancedepositService.selectZhwyAdvancedepositList(zhwyAdvancedeposit);
        ExcelUtil<ZhwyAdvancedeposit> util = new ExcelUtil<ZhwyAdvancedeposit>(ZhwyAdvancedeposit.class);
        util.exportExcel(response, list, "预存款信息数据");
    }

    /**
     * 获取预存款信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zhwyAdvancedepositService.selectZhwyAdvancedepositById(id));
    }

    /**
     * 新增预存款信息
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:add')")
    @Log(title = "预存款信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhwyAdvancedeposit zhwyAdvancedeposit)
    {
        return toAjax(zhwyAdvancedepositService.insertZhwyAdvancedeposit(zhwyAdvancedeposit));
    }

    /**
     * 修改预存款信息
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:edit')")
    @Log(title = "预存款信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhwyAdvancedeposit zhwyAdvancedeposit)
    {
        return toAjax(zhwyAdvancedepositService.updateZhwyAdvancedeposit(zhwyAdvancedeposit));
    }

    /**
     * 删除预存款信息
     */
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:remove')")
    @Log(title = "预存款信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zhwyAdvancedepositService.deleteZhwyAdvancedepositByIds(ids));
    }
}
